package net.sharpf2.api.interfaces;

import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.Methods;
import net.sharpf2.api.game.Interfaces;
import net.sharpf2.api.types.Content;

/**
 * @author Piotr Gajdowski
 * @author Rick van Biljouw
 */
public class Tabs {
    
    /**
     * Checks if tab is opened, if not tab will open
     * @param id    ID of the tab. Please use the Constants class to indicate which tab you want to open.
     */
    public static void checkAndOpen(int id){
        if (!isOpen(id)) {
            while (!isOpen(id) && !Thread.currentThread().isInterrupted()) {
                open(id);
            }
        }
    }
    
    /**
     * Opens a tab.
     * @param id    Please use the Constants class to indicate which tab you want to open.
     */
    public static void open(int id) {
        if (Constants.TAB_IDS[id] > 0 && !isOpen(id)) {
            Content<Interface> tab = Interfaces.get(Constants.GAME_INTERFACE, Constants.TAB_IDS[id]);
            if(tab != null) {
                tab.getActions().click();
                Methods.sleep(1000);
                isOpen(id);
            }
        }
    }   
    
    /**
     * Checks if the tab with the given tab ID is opened.
     * @param   tabID Please use Constants class to indicate which tab to check.
     * @return  true if tab is open, otherwise false.
     */
    public static boolean isOpen(int tabID) {
        if (tabID != Constants.TAB_LOGOUT) {
            if (Constants.TAB_IDS[tabID] > 0) {
                Content<Interface> tab = Interfaces.get(Constants.GAME_INTERFACE, Constants.TAB_IDS[tabID]);
                if (tab.getContent() != null && tab.getContent().getTextureId() != -1) {
                    return true;
                }
            }
        } else {
            boolean logOutOpen = true;
            for(int i = 0;i<Constants.TAB_IDS.length-1;i++){
                Content<Interface> tab = Interfaces.get(Constants.GAME_INTERFACE, Constants.TAB_IDS[i]);
                if (tab != null && tab.getContent().getTextureId() != -1) {
                    logOutOpen = false;
                }
            }
            return logOutOpen;
        }
        return false;
    }
        
    
    public static class Constants {
        public static final int GAME_INTERFACE = 548;
        public static final int TAB_ATTACK = 0;
	public static final int TAB_ACHIEVEMENTS = 1;
	public static final int TAB_STATS = 2;
	public static final int TAB_QUESTS = 3;
	public static final int TAB_INVENTORY = 4;
	public static final int TAB_EQUIPMENT = 5;
	public static final int TAB_PRAYER = 6;
	public static final int TAB_MAGIC = 7;
	public static final int TAB_SUMMONING = 8;
	public static final int TAB_FRIENDS = 9;
	public static final int TAB_FRIENDSCHAT = 10;
	public static final int TAB_CLAN = 11;
	public static final int TAB_OPTIONS = 12;
	public static final int TAB_EMOTE = 13;
	public static final int TAB_MUSIC = 14;
	public static final int TAB_NOTES = 15;
	public static final int TAB_LOGOUT = 16;
        public static final int[] TAB_IDS = { 128, 129, 130, 131, 132, 133, 134, 135, 98, 99, 100, 101, 102, 103, 104, 105, 181 };
    }    
    
}
